package com.wu.project.admin.service.impl;

import cn.hutool.core.date.format.FastDateFormat;
import com.wu.project.admin.component.JsonResult;
import com.wu.project.admin.dao.TaskInfoDao;
import com.wu.project.admin.entity.PageData;
import com.wu.project.admin.entity.TaskInfoDTO;
import com.wu.project.admin.entity.TaskInfoQueryVO;
import com.wu.project.admin.service.TaskInfoService;
import com.wu.project.admin.util.CommonResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author Sansan
 */
@Service
public class TaskInfoServiceImpl implements TaskInfoService {

    private final FastDateFormat fastDateFormat = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");

    @Autowired
    private TaskInfoDao taskInfoDao;

    @Override
    public JsonResult getTaskInfoList(TaskInfoQueryVO taskInfoQueryVO) {
        Integer offset = CommonResultUtil.Instance.getOffset(taskInfoQueryVO.getPageNum(), taskInfoQueryVO.getPageSize());
        taskInfoQueryVO.setOffset(offset);

        List<TaskInfoDTO> taskInfoList = taskInfoDao.queryTaskInfoList(taskInfoQueryVO);
        Integer total = taskInfoDao.queryTaskInfoListSize(taskInfoQueryVO);

        PageData pageData = new PageData();
        pageData.setPageNum(taskInfoQueryVO.getPageNum());
        pageData.setPageSize(taskInfoQueryVO.getPageSize());
        pageData.setTotal(total);
        pageData.setData(taskInfoList);
        return CommonResultUtil.Instance.success(pageData);
    }

    @Override
    public JsonResult addTaskInfo(TaskInfoDTO taskInfoDTO) {
        String format = fastDateFormat.format(new Date());
        taskInfoDTO.setTaskInfoRelease(format);
        taskInfoDTO.setTaskInfoStatus("1");
        Integer state = taskInfoDao.addTaskInfo(taskInfoDTO);
        if (state > 0) {
            return CommonResultUtil.Instance.success();
        }
        return CommonResultUtil.Instance.error();
    }

    @Override
    public JsonResult getTaskInfoOne(Long taskInfoId) {
        TaskInfoDTO taskInfoDTO = taskInfoDao.queryTaskInfoOne(taskInfoId);
        return CommonResultUtil.Instance.success(taskInfoDTO);
    }

    @Override
    public JsonResult updateTaskInfo(TaskInfoDTO taskInfoDTO) {
        Integer state = taskInfoDao.updateTaskInfo(taskInfoDTO);
        if (state > 0) {
            return CommonResultUtil.Instance.success();
        }
        return CommonResultUtil.Instance.error();
    }

    @Override
    public JsonResult deleteTaskInfo(TaskInfoDTO taskInfoDTO) {
        Integer state = taskInfoDao.deleteTaskInfo(taskInfoDTO);
        if (state > 0) {
            return CommonResultUtil.Instance.success();
        }
        return CommonResultUtil.Instance.error();
    }
}
